/**
 * 定时任务管理器
 */
import {Log} from "../log/Log.js";

export class Interval {
    _itvBox = [];
    time = 0;
    constructor() {
        Log.log('任务组件初始化');

    }
    /**
     * 创建定时任务
     * @param fn
     */
    set(fn){
        if(typeof fn != 'function') throw new Error('参数类型错误: fn');
        let id = setInterval(fn, this.time);
        this._itvBox[id] = id;
        Log.log(this._itvBox);
        return id;
    }

    /**
     *
     * @param fn
     */
    setTimeout(fn){
        if(typeof fn != 'function') throw new Error('参数类型错误: fn');
        setTimeout(fn, this.time);
    }

    /**
     * 关闭定时任务
     */
    stop(intervalId){
        if(!intervalId) throw new Error('任务ID必须');
        clearInterval(this._itvBox[intervalId]);
    }

    /**
     * 关闭定时任务
     */
    stopAll(){
        for (let intervalId=0; intervalId<this._itvBox.length; intervalId++){
            clearInterval(this._itvBox[intervalId]);
        }
    }

    /**
     * 查询
     * @returns {[]}
     */
    getAllItvBox(){
        return this._itvBox;
    }
}